package com.yingside.gui.review;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MoviePanel extends JPanel {

    private LovoButton btnAdd,btnDel,btnEdit;
    private LovoTable table;

    private MovieDao movieDao = new MovieDao();

    public MoviePanel() {
        this.setLayout(null);
        initTable();
        initData();
        initButton();
    }

    public void initTable(){
        table = new LovoTable(this,
                new String[]{"序号","电影名称","电影票价","导演"},
                new String[]{"id","movieName","price","director"},
                "id");

        table.setSizeAndLocation(30,30,400,200);
    }

    public void initData(){
        List<Movie> list = movieDao.list();
        table.updateLovoTable(list);
    }

    public void initButton(){
        btnAdd = new LovoButton("新增",50,240,this);
        btnDel = new LovoButton("删除",150,240,this);
        btnEdit = new LovoButton("修改",250,240,this);

        btnDel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = table.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行删除操作");
                    return;
                }

                int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗?");
                if(m == 0){
                    boolean flag = movieDao.del(key);

                    if(flag){
                        JOptionPane.showMessageDialog(null,"删除成功!");
                        //删除成功之后要重新将数据和table绑定
                        initData();
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"删除失败！");
                    }
                }

            }
        });
    }
}
